package com.rzico.basics.controller.member;

import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Turntable;
import com.rzico.basics.entity.TurntableRecord;
import com.rzico.basics.model.PrizeVo;
import com.rzico.basics.service.MemberService;
import com.rzico.basics.service.TurntableRecordService;
import com.rzico.basics.service.TurntableService;
import com.rzico.util.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 大转盘控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-25
 */
@Api(description = "大转盘接口")
@RestController
@RequestMapping("/turntable")
public class TurntableController extends BaseController {

    @Autowired
    private TurntableService turntableService;

    @Autowired
    private TurntableRecordService turntableRecordService;

    @Autowired
    private MemberService memberService;

    @ApiOperation("获取当前大转盘的信息")
    @GetMapping(value = "/info")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "大转盘ID", dataType = "Long", paramType = "query")
    })
    public CommResult<Map<String, Object>> info(Long id) {
        Turntable turntable = turntableService.findById(id);
        if (turntable == null) {
            return CommResult.error("当前大转盘不存在");
        }
        // 判断 当前大转盘 是否 已经开始
        Date startTime = turntable.getStartTime();
        if (startTime != null && new Date().before(startTime)) {
            return CommResult.error("大转盘抽奖活动暂未开始");
        }
        // 判断 当前大转盘 是否 已经结束
        Date endTime = turntable.getEndTime();
        if (endTime != null && new Date().after(endTime)) {
            return CommResult.error("大转盘抽奖活动已结束");
        }

        // 获取当前 大转盘信息
        Map<String, Object> map = new HashMap<>();
        // 开始与结束时间
        map.put("startTime", DateUtils.getDateStr(startTime, 0));
        map.put("endTime", DateUtils.getDateStr(endTime, 0));

        Member member = memberService.getCurrent();
        // 剩余次数
        int attendRemain = turntableRecordService.allowAttend(turntable, member.getId());
        map.put("attendRemain", attendRemain);

        // 获取当前用户 中奖记录，返回对应的奖品
        List<PrizeVo> prizeVos = turntableRecordService.findByMemberId(member.getId(), id);
        map.put("myPrizeList", prizeVos);
        // 获取 中奖信息记录
        map.put("recordList", turntableRecordService.findCurrentRecord(id));

        // 获取奖品规则
        map.put("intro", turntable.getIntro());
        return CommResult.success(map);
    }

    @ApiOperation("大转盘进行抽奖")
    @GetMapping(value = "/luckDraw")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "大转盘ID", dataType = "Long", paramType = "query")
    })
    public CommResult<TurntableRecord> luckDraw(Long id) {
        Turntable turntable = turntableService.findById(id);
        if (turntable == null) {
            return CommResult.error("当前大转盘不存在");
        }
        // 判断 当前大转盘 是否 已经开始
        Date startTime = turntable.getStartTime();
        if (startTime != null && new Date().before(startTime)) {
            return CommResult.error("大转盘抽奖活动暂未开始");
        }
        // 判断 当前大转盘 是否 已经结束
        Date endTime = turntable.getEndTime();
        if (endTime != null && new Date().after(endTime)) {
            return CommResult.error("大转盘抽奖活动已结束");
        }
        Member member = memberService.getCurrent();
        int attendRemain = turntableRecordService.allowAttend(turntable, member.getId());
        if (turntable.getPerAttendMax() != 0 && attendRemain <= 0) {
            if ("1".equals(turntable.getFqcy())) {
                return CommResult.error("您今天已经参与该活动，请明天再来继续参加。");
            } else if ("2".equals(turntable.getFqcy())) {
                return CommResult.error("您本周已经参与该活动，请下周再来继续参加。");
            } else if ("3".equals(turntable.getFqcy())) {
                return CommResult.error("您本月已经参与该活动，请下个月再来继续参加。");
            }
        }
        try {
            TurntableRecord record = turntableService.attendTurntable(turntable, member);
            return CommResult.success(record);
        }catch (Exception e){
            return CommResult.error(e.getMessage());
        }

    }

    @ApiOperation("确认领奖")
    @GetMapping(value = "/drawLuckSubmit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "抽奖记录Id", dataType = "Long", paramType = "query")
    })
    public CommResult<Object> drawLuckSubmit(Long recordId) {
        TurntableRecord record = turntableRecordService.findById(recordId);
        if (record == null) {
            return CommResult.error("当前记录不存在！");
        }

        if (record.getIsDraw() == 1) {
            return CommResult.error("已领取奖励，不可重复领取！");
        }

        Member current = memberService.getCurrent();
        if (!current.getId().equals(record.getMemberId())) {
            return CommResult.error("非该会员的奖品！");
        }
        turntableRecordService.drawLuckSubmit(record, current);
        return CommResult.success();
    }

}
